//
//  RCUserScoreVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-8-22.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCUserScoreVC.h"
#import "RCUserVCScoreCell.h"
#import "RCMyScoreListDataModel.h"
#import "IManager+ModulePersonCenter.h"

@interface RCUserScoreVC ()<IManagerDelegate>{
    int64_t  _reqID;
    BOOL     _refresh;
    int      _pn;
}
@property (nonatomic, retain) RCMyScoreListDataModel *model;
@property (nonatomic,retain) MBProgressHUDEx *progressHub;

@end

@implementation RCUserScoreVC
@synthesize model = _model;
@synthesize progressHub = _progressHub;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) dealloc
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    [_model release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.titleLabelText = @"我的积分";
    self.navigationItem.titleView = [self createTitleView];
    UIButton *rightBtn = [[[UIButton alloc] init] autorelease];
    [rightBtn setImage:[UIImage imageNamed:@"RightIcon_RCUserVC_Score"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem =  [self createRightBarButtonItemWithButton:rightBtn];


    // 设置tableview相关参数
    if ( baseVCTableView != nil ) {
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.backgroundColor = [UIColor clearColor];
        baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        baseVCTableView.frame = self.view.bounds;
        CGRect tableViewNewFrame = CGRectMake(baseVCTableView.frame.origin.x,baseVCTableView.frame.origin.y,baseVCTableView.frame.size.width,UIScreenHeight-20-self.navigationController.navigationBar.frame.size.height-4);
        baseVCTableView.frame = tableViewNewFrame;
        baseVCTableView.tableHeaderView = nil;
        baseVCTableView.tableFooterView = nil;
        [self addPullRefresh];
    }
    [self refreshData];
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UITableView Datasource & Delegate

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section == 0) {
        return 1;
    }else {
        return [self.model.listItems count] + 1;
    }
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath section] == 0) {
        return 80;
    }else{
        return 50;
    }
}


- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([indexPath section] == 0) {
        RCUserVCScoreHeaderCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"RCUserVCScoreHeaderCell"];
        if (nil == headerCell) {
            headerCell = [[[RCUserVCScoreHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCUserVCScoreHeaderCell"] autorelease];
        }
        [headerCell bindData:self.model];
        return headerCell;
    }else{
        RCUserVCScoreItemCell *headerCell = [tableView dequeueReusableCellWithIdentifier:@"RCUserVCScoreItemCell"];
        if (nil == headerCell) {
            headerCell = [[[RCUserVCScoreItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCUserVCScoreItemCell"] autorelease];
        }
        if ([indexPath row] == 0){
            headerCell.isFirst = YES;
            [headerCell bindData:nil];
        }
        else{
            RCMyScoreItemDataModel *item = [self.model.listItems objectAtIndex:([indexPath row]-1)];
            headerCell.isFirst = NO;
            [headerCell bindData:item];
        }
        return headerCell;
    }
  
    
}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


#pragma mark Request Data
- (void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _refresh = YES;
    _pn = 0;
    
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    _reqID = [[IManager sharedInstance] requestUserScorewithPn:_pn withRn:10 withDelegte:self];
    
    
}

- (void) loadMoreData
{
    _refresh = NO;
    
    //    NSString *page = [NSString stringWithFormat:@"%d",_pn];
    _reqID = [[IManager sharedInstance] requestUserScorewithPn:_pn withRn:10 withDelegte:self];
}


#pragma mark Imanager Delegate


- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    if (_reqID == requestId) {
        if (_refresh) {
            [_progressHub removeFromSuperview];
            [_progressHub release];
            _progressHub = nil;
            [self finishRefreshingData];
            _refresh = NO;
        }else{
            [self finishLoadMoreData];
        }
    }
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    if (dataModel.errorNO == RCErrorTypeNoError) {
        RCMyScoreListDataModel *resultList = (RCMyScoreListDataModel *)dataModel;
        if (_refresh) {
            [_progressHub removeFromSuperview];
            [_progressHub release];
            _progressHub = nil;
            self.model = resultList;
            [self finishRefreshingData];
            _refresh = NO;
            
//            [baseVCTableView reloadData];
        }else{
            self.model.pn = resultList.pn;
            self.model.rn = resultList.rn;
            self.model.total = resultList.total;
            [self.model.listItems addObjectsFromArray:resultList.listItems];
            
            [baseVCTableView reloadData];
            [self finishLoadMoreData];
        }
        
        
        
        NSInteger currentTotal = [self.model.listItems count];
        _pn = currentTotal;
        
        if (currentTotal < self.model.total) {
            [self addPullLoadMore];
        }else{
            [self noMoreData];
        }
    }
    [baseVCTableView reloadData];
}

@end
